package cn.lwj.controller;

import cn.lwj.common.BaseContext;
import cn.lwj.common.R;
import cn.lwj.pojo.ShoppingCart;
import cn.lwj.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.logging.LoggingPermission;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {


    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加菜品或订单至购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("新增的购物车数据：" + shoppingCart.toString());

        //        0.获取 菜品id 套餐id 用户id
//        设置创建时间
        shoppingCart.setCreateTime(LocalDateTime.now());

        long userId = BaseContext.getCurrentId(); // 用户id

        Long dishId = shoppingCart.getDishId();

        shoppingCart.setUserId(userId);

        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>(); // 条件查询对象

        lqw.eq(ShoppingCart::getUserId,userId);

//        1.判断添加的是菜品还是套餐
        if(setmealId != null){
//            说明添加是套餐
            lqw.eq(ShoppingCart::getSetmealId,setmealId);
        }

        if(dishId != null){
//            说明添加是菜品
            lqw.eq(ShoppingCart::getDishId,dishId);
        }

//        2.判断添加的菜品或者套餐是否存在 如果存在数量+1即可
        ShoppingCart shopcart = shoppingCartService.getOne(lqw);

        if(shopcart != null){
//            说明菜品或套餐已经添加过购物车 number+1 即可
            Integer number = shopcart.getNumber();
            shopcart.setNumber(number+1);
            shoppingCartService.updateById(shopcart);
        }else {
            //        3.否则就是新数据 直接添加即可
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            shopcart = shoppingCart;
        }

        return R.success(shopcart);

    }


    /**
     * 获取当前用户的购物车数据
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){

//      1.获取当前用户id
        long userId = BaseContext.getCurrentId();

//      2.创建条件查询对象 根据当前用户id 查询购物车数据
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        lqw.orderByDesc(ShoppingCart::getCreateTime);

//      3.根据查询购物车列表
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lqw);

        return R.success(shoppingCartList);
    }


    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> removeShoppingCart(){

//        1.获取当前用户id
        long userId = BaseContext.getCurrentId();


//        2.根据用户id清空购物车数据
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);

//        3.根据用户id清空购物车数据
        shoppingCartService.remove(lqw);

        return R.success("购物车清空成功");

    }

}
